package cn.dao.impl;

import cn.dao.BaseDao;
import cn.dao.UserDao;
import cn.entity.User;
import cn.utils.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.io.Serializable;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public Integer register(User user) {
        Serializable result = null;
        Integer id = 0;
        try {
            result = currentSession().save(user);
            id = (Integer) result;
        } catch (HibernateException e) {
            e.printStackTrace();
            id = 0;
        }

        return id;
    }

    /**
     * 新增User记录
     * @param user
     */
    @Override
    public void save(User user) {
        Transaction tx = null;
        try {
            tx = HibernateUtil.getTransaction(currentSession());
            currentSession().save(user);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
    }

    @Override
    public List<User> findByUserName(String userName) {
        String hql = "from User user where user.username = :uname";

        return currentSession().createQuery(hql).setString("uname",userName).list();
    }

    @Override
    public Long findUserCount() {

        return (Long) currentSession().createQuery("select count(id) from User").uniqueResult();
    }

    @Override
    public List<User> findPage(Integer pageNo,Integer pageSize) {

        return currentSession().createQuery("from User order by id").setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
    }

    @Override
    public User login(String username) {

        return (User)currentSession().createQuery("from User where name=?").setString(0,username).uniqueResult();
    }

    /**
     * 根据id查询User    get（）
     * @param id
     * @return
     */
    @Override
    public User get(Integer id) {
        Transaction tx = null;
        User user = new User();
        try {
            tx = HibernateUtil.getTransaction(currentSession());
            user = (User)currentSession().get(User.class,id);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
        return  user;
    }

    /**
     * 根据id查User  load（）
     * @param id
     * @return
     */
    @Override
    public User load(Integer id) {
        Transaction tx = null;
        User user = new User();
        try {
            tx = HibernateUtil.getTransaction(currentSession());
            user = (User)currentSession().load(User.class,id);
            System.out.println(user.getName());
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
        return  user;
    }

    /**
     * 删除User记录
     * @param user
     */
    @Override
    public void delete(User user) {
        Transaction tx = null;
        try {
            tx = HibernateUtil.getTransaction(currentSession());
            currentSession().delete(user);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
    }



    /**
     * 新增或修改User  saveOrUpdate（）
     * @param user
     */
    @Override
    public void saveOrUpdate(User user) {
        Transaction tx = null;
        try {
            tx = HibernateUtil.getTransaction(currentSession());
            currentSession().saveOrUpdate(user);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
    }

    /**
     * 修改User信息  merge（）
     * @param user
     */
    @Override
    public void merge(User user) {
        Transaction tx = null;
        try {
            tx = HibernateUtil.getTransaction(currentSession());
            currentSession().merge(user);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
    }

    /**
     * 修改User记录  update()
     * @param user
     */
    @Override
    public void update(User user) {
        Transaction tx = null;
        try {
            tx = HibernateUtil.getTransaction(currentSession());
            currentSession().update(user);
            HibernateUtil.commit(tx);
        } catch (HibernateException e) {
            e.printStackTrace();
            HibernateUtil.rollback(tx);
        }
    }

    @Override
    public List<User> findAllUser() {
        return currentSession().createQuery("from User").list();
    }
}
